package com.jshoperxms.components.netease.im;

import com.jshoperxms.components.netease.CheckSumBuilder;
import com.jshoperxms.components.netease.JPNeteaseKey;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * Created by sdywcd on 2018/7/9.
 * Des:推送通知类
 */
@Component
public class PushIM {

    /**
     * 消息推送者accid
     */
    private final static String PUSHMASTERACCID="jppushadmin-1";



    //随机数
    private static final String NONCE="00112233445566778811";


    /**
     * 学生预订陪练后台推送给老师
     * @param to
     * @param sendMsg
     */
    public void pushToTeacherWhenStudentBookingSparring(String to,String sendMsg){

        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(IMUrl.sendMsg);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("from", PUSHMASTERACCID));
        nvps.add(new BasicNameValuePair("ope", "0"));
        nvps.add(new BasicNameValuePair("to", to));
        nvps.add(new BasicNameValuePair("type", "0"));
        nvps.add(new BasicNameValuePair("body", "{\"msg\":'"+sendMsg+"'}"));
        nvps.add(new BasicNameValuePair("option","{\"push\":true,\"roam\":true,\"history\":false,\"sendersync\":true,\"route\":false,\"badge\":true,\"needPushNick\":true}"));
        nvps.add(new BasicNameValuePair("pushcontent",sendMsg));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            // 执行请求
            HttpResponse response = httpclient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    /**
     * 当预订陪练成功后推送给学生
     * @param to
     * @param sendMsg
     */
    public void pushToStudentWhenBookingSparringSuccess(String to,String sendMsg){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(IMUrl.sendMsg);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("from", PUSHMASTERACCID));
        nvps.add(new BasicNameValuePair("ope", "0"));
        nvps.add(new BasicNameValuePair("to", to));
        nvps.add(new BasicNameValuePair("type", "0"));
        nvps.add(new BasicNameValuePair("body", "{\"msg\":'"+sendMsg+"'}"));
        nvps.add(new BasicNameValuePair("option","{\"push\":true,\"roam\":true,\"history\":false,\"sendersync\":true,\"route\":false,\"badge\":true,\"needPushNick\":true}"));
        nvps.add(new BasicNameValuePair("pushcontent",sendMsg));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            // 执行请求
            HttpResponse response = httpclient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 老师执教审核中推送
     * @param to
     */
    public void pushToTeacherVerifyQualificationsNotice(String to){
        String sendMsg="尊敬的老师您好，我们已经收到了您的执教申请，我们会在第一时间查阅您的资料，并联系您!";
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(IMUrl.sendMsg);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("from", PUSHMASTERACCID));
        nvps.add(new BasicNameValuePair("ope", "0"));
        nvps.add(new BasicNameValuePair("to", to));
        nvps.add(new BasicNameValuePair("type", "0"));
        nvps.add(new BasicNameValuePair("body", "{\"msg\":'"+sendMsg+"'}"));
        nvps.add(new BasicNameValuePair("option","{\"push\":true,\"roam\":true,\"history\":false,\"sendersync\":true,\"route\":false,\"badge\":true,\"needPushNick\":true}"));
        nvps.add(new BasicNameValuePair("pushcontent",sendMsg));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            // 执行请求
            HttpResponse response = httpclient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 推送上课前5分钟通知给老师
     * @param to
     * @param sendMsg
     */
    public void pushToTeacherRemding5min(String to,String sendMsg){
        CloseableHttpClient httpclient = HttpClients.createDefault();
        HttpPost httpPost=new HttpPost(IMUrl.sendMsg);
        String curTime=String.valueOf((new Date()).getTime()/1000L);
        String checkSum= CheckSumBuilder.getCheckSum(JPNeteaseKey.SECRET,NONCE,curTime);
        // 设置请求的header
        httpPost.addHeader("AppKey", JPNeteaseKey.APPKEY);
        httpPost.addHeader("Nonce", NONCE);
        httpPost.addHeader("CurTime", curTime);
        httpPost.addHeader("CheckSum", checkSum);
        httpPost.addHeader("Content-Type", "application/x-www-form-urlencoded;charset=utf-8");
        // 设置请求的参数
        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
        nvps.add(new BasicNameValuePair("from", PUSHMASTERACCID));
        nvps.add(new BasicNameValuePair("ope", "0"));
        nvps.add(new BasicNameValuePair("to", to));
        nvps.add(new BasicNameValuePair("type", "0"));
        nvps.add(new BasicNameValuePair("body", "{\"msg\":'"+sendMsg+"'}"));
        nvps.add(new BasicNameValuePair("option","{\"push\":true,\"roam\":true,\"history\":false,\"sendersync\":true,\"route\":false,\"badge\":true,\"needPushNick\":true}"));
        nvps.add(new BasicNameValuePair("pushcontent",sendMsg));
        try {
            httpPost.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
            // 执行请求
            HttpResponse response = httpclient.execute(httpPost);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
